home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d15 / mewin.arc / LEVELS_F.CMD < prev    next >
OS/2 REXX Batch file  |  1991-03-12  |  5KB  |  208 lines

  1. ;  levels_f.cmd,  MicroEmacs,  24 Feb 90,  S.D. Maley
  2. ;
  3. ;  collapse and expand display
  4. ;  of lines between matching "fences"
  5. ;  for Fortran source code files
  6. ;
  7. ;  NOTE: you must be on a fence token when you invoke collapse
  8. ;
  9. set %fences_1 "IDE"
  10. set %fences_2 "FON"
  11. set %fences_3 "  D"
  12. set %fence_id "(     "
  13. set %fence_e4 "     ID"
  14. set %fence_e5 "IDFO"
  15. set %fence_e6 "FO"
  16.  
  17. store-procedure chk-fences
  18. ;-- presumes caller has placed us at start of "word"
  19. ;-- returns %status= 0 (not fence), 1 (IF or DO), -1 (END), 13 (DO <label>)
  20.   set %status 0
  21.   set %i &sindex %fences_1 &upper &chr $curchar
  22.   !if &less %i 1
  23.     !return
  24.   !else
  25.     forward-character
  26.     !if ¬ &sequ &mid %fences_2 %i 1 &upper &chr $curchar
  27.       !return
  28.     !else
  29.       forward-character
  30.       set %chr &upper &chr $curchar
  31.       set %i_id &sindex %fence_id %chr    ;-- check for IF or DO token end
  32.       !if &and &equ %i 1 &less 0 %i_id    ;   (&gre did NOT seem to work here)
  33.         execute-procedure chk-then    ;-- check whether it's a block IF
  34.       !else
  35.       !if &and &equ %i 2 &less 1 %i_id
  36.         execute-procedure chk-do-num
  37.       !else
  38.       !if ¬ &sequ &mid %fences_3 %i 1 %chr
  39.         !return
  40.       !else
  41.         execute-procedure chk-end-trail
  42.       !endif
  43.       !endif
  44.       !endif
  45.     !endif
  46.   !endif
  47. !endm
  48.  
  49. store-procedure chk-do-num
  50.   end-of-word    ;-- align with token following DO
  51.   previous-word
  52.   !if &less 57 $curchar    ;-- 57: "9"
  53.     set %status 1
  54.   !else
  55.     set %status 13
  56.     set %flabel ""    ; nil
  57.     !while &less 47 $curchar    ; q&d check, 48: "0"
  58.       set %flabel &cat %flabel &chr $curchar
  59.       forward-character
  60.     !endwhile
  61.   !endif
  62. !endm
  63.  
  64. store-procedure chk-end-trail
  65. ;-- check trailing part of END for its many possibilities
  66.   forward-character
  67.   set %i &sindex %fence_e4 &upper &chr $curchar
  68.   !if &less %i 1
  69.     !return
  70.   !endif
  71.   forward-character
  72.   set %chr &upper &chr $curchar
  73.   !if &less %i 3
  74.     set %i &sindex %fence_e5 %i 1 %chr
  75.     !if &or &less %i 0 &less 2 %i
  76.       !return
  77.     !else
  78.       forward-character
  79.       !if ¬ &sequ &mid %fence_e6 %i 1 &upper &chr $curchar
  80.         !return
  81.       !endif
  82.     !endif
  83.   !else    ;-- trailing part of END is contiguous
  84.     !if ¬ &sequ &mid %fence_e5 %i 1 %chr
  85.       !return
  86.     !endif
  87.   !endif
  88.   set %status -1
  89. !endm
  90.  
  91. set %then "THEN"
  92.  
  93. store-procedure chk-then
  94. ;-- depends on being called from chk-fences, after we know it's an IF
  95.   !force search-forward "("
  96.   !if ¬ $status
  97.     !return
  98.   !endif
  99.   backward-character
  100.   !force goto-matching-fence
  101.   !if ¬ $status
  102.     !return
  103.   !endif
  104.   next-word
  105.   set %rem ""
  106.   set %i 1
  107.   !while &and &less %i 5 ¬ &equ $curchar 13    ;-- 13: newline
  108.     set %rem &cat %rem &upper &chr $curchar
  109.     set %i &add %i 1
  110.     forward-character
  111.   !endwhile
  112.   !if &sequ %rem "THEN"
  113.     !if &less $curchar 34    ;-- token terminate valid
  114.       set %status 1        ;-- a valid block IF statement
  115.     !endif
  116.   !endif
  117. !endm
  118.  
  119. store-procedure collapse
  120.   set %bfl $curline
  121.   execute-procedure goto-fence-match
  122.   !if %status
  123.     !if &less $curline %bfl
  124.       set %efl %bfl
  125.     !else
  126.       set %efl $curline
  127.       set $curline %bfl
  128.     !endif
  129.     update-screen
  130.     set %whalf &div $wline 2
  131.     !if &less %whalf $cwline
  132.       &sub $cwline %whalf move-window-down
  133.     !endif
  134.     split-current-window
  135.     !if &less $cwline $wline
  136.       &sub $wline $cwline shrink-window
  137.     !endif
  138.     next-window
  139.     set $curline %efl
  140.     &sub $cwline 1 move-window-down
  141.   !else
  142.     write-message "Unmatched"
  143.   !endif
  144. !endm
  145.  
  146. store-procedure expand
  147.   delete-window
  148.   set $curline %bfl
  149. !endm
  150.  
  151.  
  152. store-procedure goto-fence-match
  153. ;-- for fences with embedded whitespace, cursor must be on the first "word"
  154.   end-of-word
  155.   previous-word
  156.   execute-procedure chk-fences
  157.   !if &equ %status 0
  158.     set %status FALSE
  159.     write-message "Place cursor on a valid Fortran fence."
  160.     !return
  161.   !endif
  162.   !if &less 1 %status
  163.     !while &less 7 $curcol    ;-- restrict search to Fortran label columns
  164.       !force search-forward %flabel
  165.       !if ¬ $status
  166.         !break
  167.       !endif
  168.     !endwhile
  169.     set %status $status
  170.   !else
  171.     set %nmatch %status
  172.     !if &less %status 0    ;-- find match for END
  173.       !while ¬ &equ %nmatch 0
  174.         !force previous-line
  175.         !if ¬ $status
  176.           set %status FALSE
  177.           !return
  178.         !endif
  179.         execute-procedure tally-fence    ;-- bumps %nmatch
  180.       !endwhile
  181.     !else            ;-- find match for IF or DO
  182.       !while ¬ &equ %nmatch 0
  183.         !force next-line
  184.         !if ¬ $status
  185.           goto %bfl
  186.           set %status FALSE
  187.           !return
  188.         !endif
  189.         execute-procedure tally-fence
  190.       !endwhile
  191.     !endif
  192.     set %status TRUE
  193.   !endif
  194. !endm
  195.  
  196. store-procedure tally-fence
  197.   !force set $curcol 5
  198.   !if $status
  199.     next-word
  200.     execute-procedure chk-fences
  201.     set %nmatch &add %status %nmatch
  202.   !endif
  203. !endm
  204.  
  205. ;-----------------------------------------------
  206. macro-to-key expand    M-FNC    ;-- <Meta> <Ins>
  207. macro-to-key collapse    M-FND    ;-- <Meta> <Del>
  208.